<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    div{
        padding: 150px;
        width: 1600px;
        height: 750px;
        background-color: #23d1e4;
    }
    form{
        width: 500px;
        height: 400px;
        padding: 50px 25px;
        background-color: #f5f5f5;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;

        justify-content: space-evenly;
        align-items: center;
        /* align-content: space-between; */
    }
    input{
        display: block;
        background-color: #fff;
        color: black;
        width: 300px;
        height: 30px;
    }
    input[type=submit]{
        background-color: blue;
        color:white;
        border:none;
        cursor: pointer;
    }
</style>
<body>
    <div>
        <form>
            <h2>欢迎注册</h2>
            <p>提示注册账号的同时系统会自动生成钱包</p>
            <input type="text" name="uaername" placeholder="用户名">
            <input type="text" name="password" placeholder="密码">
            <input type="text" name="rpassword" placeholder="密码">
            <input type="text" name="nickname" placeholder="昵称">
            <span></span> 
            <input type="submit" value="提交">
            
        </form>
    </div>
</body>
</html>
<script src="./axios.js"></script>
<script>
    //      表单事件： oForm.onsubmit=function(e){}   阻止默认行为(跳转)
    var oTxt1 = document.querySelector("[name=uaername]")
    var oTxt2 = document.querySelector("[name=password]")
    var oTxt3 = document.querySelector("[name=rpassword]")
    var oTxt4 = document.querySelector("[name=nickname]")
    var oBtn = document.querySelector("[type=submit]");
    var oForm = document.querySelector("form");
    var oSpan = document.querySelector("span");
    oForm.onsubmit = function(ev){ //ev: 事件对象
        // 1.阻止表单的默认跳转行为
        ev.preventDefault();
        // 2.收集用户信息
        var username = oTxt1.value
        var password = oTxt2.value
        var rpassword = oTxt3.value
        var nickname = oTxt4.value
        // 3.非空校验(如果有一个是空的，就提示填写完成的信息，终止程序继续向下执行)
        if(!username || !password || !rpassword || !nickname) return alert("请填写完整信息")
        // 4.密码是否一致校验
        if(password != rpassword) return alert("密码不一致");
        // 5.发送axios请求
        axios({
            method:"post",
            url:"http://localhost:8888/users/register",
            headers:{
                "content-type":"application/x-www-form-urlencoded"
            },
            data:{
                username,
                password,
                rpassword,
                nickname,
            }
        }).then(res=>{
            console.log(res);
            // 根据状态判定注册是否成功 和 失败
            if(res.data.code != 1){
                oSpan.innerText = '亲，注册失败，请重新注册'
                oSpan.style.color = 'red';
                // 终止继续向下执行
                return;
            }
            //如果能执行到这里，说明code=1
            alert("注册成功")
            //跳转到登录
            location.href = 'login.html'
        })


    }
</script>